package action.credit;

import service.credit.ApplyCustomRelationInsertService;
import service.credit.CreditApplyPersonalService;
import util.Log;
import util.Tool;

import com.opensymphony.xwork2.ModelDriven;

import core.action.BaseAction;
import exception.Exceptions;
import model.credit.Applycustomrelation;
import model.credit.Creditapplypersonal;

/**
 * 存储个人申请信息
 * @author FOTF
 * */
public class CreditPersonalApplyInsertAction extends BaseAction implements
		ModelDriven<Creditapplypersonal> {

	//使用ModelDriven注入
	private Creditapplypersonal creditApplyPersonal = new Creditapplypersonal();
	
	private CreditApplyPersonalService creditApplyPersonalService;
	private ApplyCustomRelationInsertService applyCustomRelationInsertService;
	
	private String keepInforHidden;  //保证人编号
	private String hypothecateHidden;//抵押物品编号
	private String pledgeHidden;//质押物品编号
	
	private String keepInforHiddenRPT;//保证人类型
	private String hypothecateHiddenRPT;//抵押人类型
	private String pledgeHiddenRPT;//质押人类型
	
	private String hypothecateHiddenPI;//抵押人编号
	private String pledgeHiddenPI;//质押人编号
	
	/**
	 * 个人申请信息保存
	 * */
	public String execute() throws Exception {
		String modes = this.creditApplyPersonal.getLoanofmode();
		
		if(creditApplyPersonalService.getCount(this.creditApplyPersonal.getApplyid()) != 0){
			throw new Exceptions("对不起，该申请号码已经存在！");
		}
		if(!this.creditApplyPersonal.getLoanofmode().equals("4")){
			if(modes.contains("3") && this.keepInforHidden != null){
				String[] keepInforH = this.substringStr4(Tool.splitArrayHtml(keepInforHidden));
				String[] keepInforHRPT = Tool.splitArrayHtml(keepInforHiddenRPT);
				int[] keepInforHRPTi = this.c2i(keepInforHRPT);
				for(int i = 0; i < keepInforH.length; i++){
					Applycustomrelation www = new Applycustomrelation();
					www.setApplyid(creditApplyPersonal.getApplyid());
					www.setClientid(creditApplyPersonal.getClientid());
					www.setClienttype(1);
					www.setRelationtype(3);
					www.setRelationpeopleid(Integer.parseInt(keepInforH[i]));
					www.setRelationpeopletype(keepInforHRPTi[i]);
					www.setState("1");
					applyCustomRelationInsertService.save(www);
				}
				creditApplyPersonal.setModeB(1);
			}
			if(modes.contains("1") && this.hypothecateHidden != null){
				String[] hypothecateH = Tool.splitArrayHtml(hypothecateHidden);
				String[] hypothecateHRPT = Tool.splitArrayHtml(hypothecateHiddenRPT);
				int[] hypothecateHRPTi = this.c2i(hypothecateHRPT);
				String[] hypothecateHPI = this.substringStr4(Tool.splitArrayHtml(hypothecateHiddenPI));
				for(int i = 0; i < hypothecateH.length; i++){
					Applycustomrelation www = new Applycustomrelation(creditApplyPersonal.getApplyid(), creditApplyPersonal.getClientid(), 1, 1,  Integer.parseInt(hypothecateHPI[i]),  hypothecateHRPTi[i],  Integer.parseInt(hypothecateH[i]));
					www.setState("1");
					applyCustomRelationInsertService.save(www);
				}
				creditApplyPersonal.setModeD(1);
			}
			if(modes.contains("2") && this.pledgeHidden != null){
				String[] pledgeH = Tool.splitArrayHtml(pledgeHidden);
				String[] pledgeHRPT = Tool.splitArrayHtml(pledgeHiddenRPT);
				int[] pledgeHRPTi = this.c2i(pledgeHRPT);
				String[] pledgeHPI = this.substringStr4(Tool.splitArrayHtml(pledgeHiddenPI));
				for(int i = 0; i < pledgeH.length; i++){
					Applycustomrelation www = new Applycustomrelation(creditApplyPersonal.getApplyid(), creditApplyPersonal.getClientid(), 1, 2,  Integer.parseInt(pledgeHPI[i]),  pledgeHRPTi[i],  Integer.parseInt(pledgeH[i]));
					www.setState("1");
					applyCustomRelationInsertService.save(www);
				}
				creditApplyPersonal.setModeZ(1);
			}
			}else{
				creditApplyPersonal.setModeX(1);
			}
		this.creditApplyPersonalService.save(creditApplyPersonal);
		Log.save("插入", "向个人贷款申请表中插入一条新的记录，插入的新记录的申请号码为：" + creditApplyPersonal.getApplyid() + "，贷款方式为：" + creditApplyPersonal.getLoanofmode(), "对应id编号为：" + creditApplyPersonal.getId());
		MessageJump("贷款申请信息增加成功", "creditApplyAction!go_in_personal");
		return "message";
	}

	public ApplyCustomRelationInsertService getApplyCustomRelationInsertService() {
		return applyCustomRelationInsertService;
	}

	public void setApplyCustomRelationInsertService(
			ApplyCustomRelationInsertService applyCustomRelationInsertService) {
		this.applyCustomRelationInsertService = applyCustomRelationInsertService;
	}

	public CreditApplyPersonalService getCreditApplyPersonalService() {
		return creditApplyPersonalService;
	}

	public void setCreditApplyPersonalService(
			CreditApplyPersonalService creditApplyPersonalService) {
		this.creditApplyPersonalService = creditApplyPersonalService;
	}

	@Override
	public Creditapplypersonal getModel() {
		return creditApplyPersonal;
	}

	public String getKeepInforHidden() {
		return keepInforHidden;
	}

	public void setKeepInforHidden(String keepInforHidden) {
		this.keepInforHidden = keepInforHidden;
	}

	public String getHypothecateHidden() {
		return hypothecateHidden;
	}

	public void setHypothecateHidden(String hypothecateHidden) {
		this.hypothecateHidden = hypothecateHidden;
	}

	public String getPledgeHidden() {
		return pledgeHidden;
	}

	public void setPledgeHidden(String pledgeHidden) {
		this.pledgeHidden = pledgeHidden;
	}

	public String getKeepInforHiddenRPT() {
		return keepInforHiddenRPT;
	}

	public void setKeepInforHiddenRPT(String keepInforHiddenRPT) {
		this.keepInforHiddenRPT = keepInforHiddenRPT;
	}

	public String getHypothecateHiddenRPT() {
		return hypothecateHiddenRPT;
	}

	public void setHypothecateHiddenRPT(String hypothecateHiddenRPT) {
		this.hypothecateHiddenRPT = hypothecateHiddenRPT;
	}

	public String getPledgeHiddenRPT() {
		return pledgeHiddenRPT;
	}

	public void setPledgeHiddenRPT(String pledgeHiddenRPT) {
		this.pledgeHiddenRPT = pledgeHiddenRPT;
	}

	public String getHypothecateHiddenPI() {
		return hypothecateHiddenPI;
	}

	public void setHypothecateHiddenPI(String hypothecateHiddenPI) {
		this.hypothecateHiddenPI = hypothecateHiddenPI;
	}

	public String getPledgeHiddenPI() {
		return pledgeHiddenPI;
	}

	public void setPledgeHiddenPI(String pledgeHiddenPI) {
		this.pledgeHiddenPI = pledgeHiddenPI;
	}
	
	/**
	 * 匹配身份信息
	 * 个人：数据库存储1
	 * 企业：数据库存储2
	 * */
	public int[] c2i(String[] c){
		int[] ilist = new int[c.length];
		for(int i = 0; i < c.length; i++){
			if(c[i].equals("个人")){
				ilist[i] = 1;
			}else if(c[i].equals("企业")){
				ilist[i] = 2;
			}
		}
		return ilist;
	}
	
	/**
	 * 屏蔽所有使用客户id的前四位数据
	 * 前四位数据为系统默认，数据库不存储
	 * */
	public String[] substringStr4(String[] str){
		String[] list = new String[str.length];
		for(int i = 0; i < str.length; i++){
			list[i] = str[i].substring(4, str[i].length());
		}
		return list;
	}

}
